/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.controller;

import cn.com.recloud.core.util.Result;
import cn.com.recloud.one.spring.model.KeyCloakConfig;
import cn.com.recloud.one.spring.model.KeyCloakConfigToken;
import cn.com.recloud.one.spring.model.KeyCloakToken;
import cn.com.recloud.one.spring.util.HttpConfigUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.*;


/**
 * @author kim dong
 * @date 2020-04-28
 */
@RestController
@RequestMapping("/api")
@Api(tags = "获取kc的配置")
public class AppController {

    @Value("${keycloak.auth-server-url}")
    private String serverUrl;

    @Value("${keycloak.realm}")
    private String realm;

    @Value("${keycloak.resource}")
    private String clientId;

    @Value("${recloud.kc.client}")
    private String appClientId;

    @Value("${recloud.kc.secret}")
    private String secret;

    @GetMapping("/keyconf")
    @ApiOperation("获取kc配置")
    public Result keyCloakConfig(){
        KeyCloakConfig config = new KeyCloakConfig();
        config.setResource(clientId);
        config.setRealm(realm);
        config.setAuthServerUrl(serverUrl);
        return new Result(config);
    }

    /**
     * 获取keycloak手机端的token
     * @return
     */
    @RequestMapping("/apptoken")
    @ApiOperation("获取apptoken")
    public Result appToken(){
        KeyCloakConfigToken kcConfig = new KeyCloakConfigToken();
        kcConfig.setRealm(realm);
        kcConfig.setUrl(serverUrl);
        kcConfig.setClient(appClientId);
        kcConfig.setSecret(secret);
        KeyCloakToken kcToken = HttpConfigUtil.getKeyCloakToken(kcConfig);
        return new Result(kcToken);
    }

}
